package com.dayaokeji.rhythmschoolstudent.client.mine.bulletin;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ListFragment;
import com.dayaokeji.rhythmschoolstudent.event.BulletinEvent;
import com.dayaokeji.rhythmschoolstudent.utils.BulletinUtil;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.BulletinApi;
import com.dayaokeji.server_api.domain.Bulletin;
import com.dayaokeji.server_api.domain.MessageReceipt;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import retrofit2.Call;

/**
 * Created by Gorden on 2017/12/19.
 *
 * 通知列表
 */

public class BulletinListFragment extends ListFragment<Bulletin,BaseViewHolder> {

    private static final BulletinApi bulletinApi = ApiUtils.getApi(BulletinApi.class);
    private Call<ServerResponse<Void>> receiptCall;

    public static BulletinListFragment newInstance() {
        
        Bundle args = new Bundle();
        
        BulletinListFragment fragment = new BulletinListFragment();
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    protected Call createCall(int offset, int max) {
        return ApiUtils.getApi(BulletinApi.class).messages(offset,max);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRecyclerView();
        setAdapter(new BulletinAdapter());
        setOnItemListener();
    }

    private void setRecyclerView() {
    }

    private void setOnItemListener() {
        BaseQuickAdapter<Bulletin, BaseViewHolder> adapter = getAdapter();
        if (adapter != null) {
            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    Bulletin bulletin = ((Bulletin) adapter.getData().get(position));
                    switch (view.getId()) {
                        case R.id.tv_receipt:
                            messageReceipt(bulletin,position);
                            break;
                    }
                }
            });

            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    int size = adapter.getData().size();
                    if (position < size) {
                        if (!getActivity().isFinishing()) {
                            Bulletin bulletin = (Bulletin) adapter.getData().get(position);
                            BulletinDetailActivity.startBulletinDetailActivity(getActivity(),bulletin);
                        }
                    }
                }
            });
        }

    }

    /**
     * 消息回执请求
     * @param bulletin
     */
    private void messageReceipt(final Bulletin bulletin, final int position) {
        receiptCall = bulletinApi.receipt(new MessageReceipt(bulletin.getId(), 2));
        receiptCall.enqueue(new ServerCallback<Void>(getActivity(),"正在处理...") {
            @Override
            public void onResponse(boolean success, ServerResponse<Void> response) {
                if (success) {
                    ToastUtils.success("回执成功");
                    bulletin.setStatus(BulletinUtil.ALREADY_RECEIPT);
                    if (getAdapter() != null) {
                        getAdapter().notifyItemChanged(position);
                    }
                }
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(BulletinEvent bulletinEvent) {
        onRefresh();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        if (receiptCall != null) {
            receiptCall.cancel();
            receiptCall = null;
        }
        super.onDestroy();
    }
}
